package de.sp.games.snake12k.boxes;

import java.util.ArrayList;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

import de.sp.games.snake12k.SUtil;

public class SnakeBodyBox extends Box{
	
	private int snakeDirection;
	private int indexXNew;
	private int indexYNew;
	private ArrayList<Box> boxList;
	private Image image;
	
	private int timeValue = 0;
	private int snakeLength;
	
	public SnakeBodyBox(final int snakeDirection, final int indexX, final int indexY, ArrayList<Box> boxList) throws SlickException {
		super();
		this.indexXNew = indexX;
		this.indexYNew = indexY;
		this.snakeDirection = snakeDirection;
		this.boxList = boxList;
		
		isCollision = true;
		isChangeable = false;
		
		this.snakeLength = SUtil.snakeLength;
		image = new Image("ressource/snake_tiles.png");
		image.setFilter(Image.FILTER_NEAREST);
		
		createSnakeBox();
	}
	
	private void createSnakeBox() {
		switch (snakeDirection) {
			case SUtil.UP:
				boxImage = image.getSubImage(16, 0, 16, 16);
				boxImage.rotate(90);
				indexX = this.indexXNew;
				indexY = this.indexYNew + 1;
				x = indexX * 16 - 16;
				y = indexY * 16 - 16;
				break;
	
			case SUtil.LEFT:
				boxImage = image.getSubImage(16, 0, 16, 16);
				indexX = this.indexXNew + 1;
				indexY = this.indexYNew;
				x = indexX * 16 - 16;
				y = indexY * 16 - 16;
				break;
	
			case SUtil.RIGHT:
				boxImage = image.getSubImage(16, 0, 16, 16);
				boxImage.rotate(180);
				indexX = this.indexXNew - 1;
				indexY = this.indexYNew;
				x = indexX * 16 - 16;
				y = indexY * 16 - 16;
				break;
	
			case SUtil.DOWN:
				boxImage = image.getSubImage(16, 0, 16, 16);
				boxImage.rotate(270);
				indexX = this.indexXNew;
				indexY = this.indexYNew - 1;
				x = indexX * 16 - 16;
				y = indexY * 16 - 16;
				break;
	
			default:
				throw new IllegalArgumentException();
		}
	}
	
	//TODO: Parameter unoetig -- wegen Field For Schleife
	public void update(GameContainer gc, ArrayList<Box> boxList) throws SlickException {
		if(SUtil.isTimeMove() && !SUtil.gameOver) {
			if(timeValue == this.snakeLength) {
				timeValue = 1;
				boxList.remove(SUtil.getBoxArrayIndex(indexX, indexY, boxList));
			} else {
				timeValue++;
			}
		}
	}
	
	public void render(Graphics g) {
			super.draw(g);
	}
}
